package _04HashMap练习;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 创建一个HashMap集合，键是学生对象(_04HashMap练习.Student)，值是家庭住址(String)。存储三个键值对元素，并遍历
 */
public class Demo {
    public static void main(String[] args) {
        Map<Student,String> map = new HashMap<>();

        Student a = new Student("张三", "男", 21);
        Student b = new Student("李四", "男", 21);
        Student c = new Student("张三", "男", 20);
        Student d = new Student("张三", "男", 20);

        map.put(a,"北京");
        map.put(b,"广州");
        map.put(c,"上海");
        map.put(d,"新疆");//由于重写了hashcode()和equals()方法，c和d为同一个对象，将d放进map集合会覆盖a的value

        System.out.println(d.equals(c));
        System.out.println(d == c);


        Set<Map.Entry<Student, String>> entries = map.entrySet();
        Iterator<Map.Entry<Student, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Student, String> entry = iterator.next();
            System.out.println(entry.getKey()+"的地址为："+entry.getValue());
        }

    }
}
